package cn.iocoder.yudao.module.blog.controller.app.follow;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.blog.controller.admin.follow.vo.BlogFollowCreateReqVO;
import cn.iocoder.yudao.module.blog.controller.admin.follow.vo.BlogFollowPageReqVO;
import cn.iocoder.yudao.module.blog.controller.admin.follow.vo.BlogFollowRespVO;
import cn.iocoder.yudao.module.blog.controller.app.follow.vo.AppBlogFollowPageReqVO;
import cn.iocoder.yudao.module.blog.controller.app.follow.vo.AppBlogFollowRespVO;
import cn.iocoder.yudao.module.blog.controller.app.follow.vo.AppBlogFollowUnfollowReqVO;
import cn.iocoder.yudao.module.blog.controller.app.follow.vo.AppBlogFollowedListReqVO;
import cn.iocoder.yudao.module.blog.controller.app.space.likevo.AppBlogSpaceLikeListReqVO;
import cn.iocoder.yudao.module.blog.convert.follow.BlogFollowConvert;
import cn.iocoder.yudao.module.blog.dal.dataobject.follow.BlogFollowDO;
import cn.iocoder.yudao.module.blog.dal.dataobject.spacecomment.BlogSpaceCommentDO;
import cn.iocoder.yudao.module.blog.dal.dataobject.spacelike.BlogSpaceLikeDO;
import cn.iocoder.yudao.module.blog.service.follow.BlogFollowService;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "用户 APP - 关注")
@RestController
@RequestMapping("/blog/follow")
@Validated
@Slf4j
public class AppBlogFollowController {

    @Resource
    private BlogFollowService followService;

    @Resource
    private MemberUserApi memberUserApi;

    @PostMapping("/follow")
    @Operation(summary = "关注")
    public CommonResult<Long> Follow(@Valid @RequestBody BlogFollowCreateReqVO createReqVO) {
        return success(followService.createFollow(createReqVO));
    }

    @PostMapping("/unfollow")
    @Operation(summary = "取消关注")
    public CommonResult<Boolean> unFollow(@Valid @RequestBody AppBlogFollowUnfollowReqVO reqVO) {
        followService.unFollow(reqVO);
        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "获得关注分页")
    public CommonResult<PageResult<AppBlogFollowRespVO>> getFollowPage(@Valid AppBlogFollowPageReqVO pageVO) {
        PageResult<BlogFollowDO> pageResult = followService.getAppFollowPage(pageVO);

        // 查询用户信息
        Set<Long> memberIds = convertSet(pageResult.getList(), BlogFollowDO::getFollowMemberId);
        Map<Long, MemberUserRespDTO> memberMap = memberUserApi.getUserMap(memberIds);

        return success(BlogFollowConvert.INSTANCE.convertAppPage(pageResult, memberMap));
    }

    @PostMapping("/list")
    @Operation(summary = "获得关注列表")
    public CommonResult<List<Long>> getLikeList(@Valid @RequestBody AppBlogFollowedListReqVO reqVO){
        List<Long> list = followService.getFollowList(reqVO.getMemberId(), reqVO.getFollowedMemberIds());
        return success(list);
    }
}
